package com.systemframework.sys.service.impl;

import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.operator.Contain;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systemframework.sys.dto.request.SysDictRequestDTO;
import com.systemframework.sys.dto.response.SysDictResponseDTO;
import com.systemframework.sys.entity.SysDict;
import com.systemframework.sys.mapper.SysDictMapper;
import com.systemframework.sys.service.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author summer
 * @date 2023-04-04 16:51
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    private BeanSearcher beanSearcher;

    @Autowired
    public void setBeanSearcher(BeanSearcher beanSearcher) {
        this.beanSearcher = beanSearcher;
    }

    @Override
    public Page<SysDictResponseDTO> lists(SysDictRequestDTO sysDictRequestDTO) {
        Map<String, Object> build = MapUtils.builder().page(sysDictRequestDTO.getPageNum()-1, sysDictRequestDTO.getPageSize())
                .field(SysDictResponseDTO::getDictCode, sysDictRequestDTO.getDictCode()).op(Contain.class)
                .field(SysDictResponseDTO::getDictName, sysDictRequestDTO.getDictName()).op(Contain.class).build();
        SearchResult<SysDictResponseDTO> search = beanSearcher.search(SysDictResponseDTO.class, build);
        Page<SysDictResponseDTO> page=new Page<>(sysDictRequestDTO.getPageNum(),sysDictRequestDTO.getPageSize());
        page.setRecords(search.getDataList());
        page.setTotal(search.getTotalCount().longValue());
       return page;
    }
}
